package com.piggy.common.core.utils;

import com.piggy.common.core.enums.AreaEnum;
import com.piggy.common.core.enums.CarColorEnum;
import com.piggy.common.core.enums.CoinTypeEnum;

/*枚举工具类*/
public class EnumUtils {


    /**
     * 根据 枚举名 和枚举类型吧 获取枚举code
     *
     * @param dictLabel 枚举名
     * @param dictType  枚举类型
     */
    public static Integer getCode(String dictLabel, String dictType) {

        /*币种*/
        if ("currency".equals(dictType)) {
            return CoinTypeEnum.valueOf(dictLabel).getCode();
        }
        /*颜色*/
        if ("car_color".equals(dictType)) {

            StringBuffer sb = new StringBuffer();
            for (int j = 0; j < dictLabel.length(); j++) {
                char c = dictLabel.charAt(j);
                if ((c <= 'z' && c >= 'a') || (c <= 'Z' && c >= 'A')) {
                    sb.append(c);
                }
            }
            dictLabel = sb.toString();
            return CarColorEnum.valueOf(dictLabel).getCode();
        }
        /*区域*/
        if ("zwy_area".equals(dictType)) {
            return AreaEnum.valueOf(dictLabel).getCode();
        }

        return null;

    }


    /**
     * 根据code和枚举类型 获取枚举名
     *
     * @param code
     * @param dictType
     * @return
     */
    public static String getName(Integer code, String dictType) {

        /*币种*/
        if ("currency".equals(dictType)) {
            for (int i = 0; i < CoinTypeEnum.values().length; i++) {
                Integer codeEnum = CoinTypeEnum.values()[i].getCode();
                if (code.equals(codeEnum)) {
                    return CoinTypeEnum.values()[i].name();
                }
            }
        }
        /*颜色*/
        if ("car_color".equals(dictType)) {
            for (int i = 0; i < CarColorEnum.values().length; i++) {
                Integer codeEnum = CarColorEnum.values()[i].getCode();
                if (code.equals(codeEnum)) {
                    return CarColorEnum.values()[i].name() + CarColorEnum.values()[i].getInfo();
                }
            }
        }

        /*区域*/
        if ("zwy_area".equals(dictType)) {
            for (int i = 0; i < AreaEnum.values().length; i++) {
                Integer codeEnum = AreaEnum.values()[i].getCode();
                if (code.equals(codeEnum)) {
                    return AreaEnum.values()[i].name();
                }
            }
        }
        return null;
    }

}
